package ua.com.minus1.lookbattle;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import ua.com.minus1.lookbattle.utils.BundleValues;

/**
 * Created with IntelliJ IDEA.
 * User: Denis
 * Date: 06.10.12
 * Time: 10:08
 * To change this template use File | Settings | File Templates.
 */
public class AlertDialogFragment extends DialogFragment {

    private static final String POSITIVE_TEXT = "positiveText";
    private static final String NEGATIVE_TEXT = "negativeText";
    private Callbacks mCallbacks;

    public interface Callbacks{
        void onDialogDismiss(boolean success);
    }

    public static AlertDialogFragment newInstance(String title, String msg, String positiveText, String negativeText) {
        Bundle args = new Bundle();
        args.putString(BundleValues.TITLE, title);
        args.putString(BundleValues.MESSAGE, msg);
        args.putString(POSITIVE_TEXT, positiveText);
        args.putString(NEGATIVE_TEXT, negativeText);

        AlertDialogFragment f = new AlertDialogFragment();
        f.setArguments(args);
        return f;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        mCallbacks = (Callbacks) getActivity();
    }

    @Override
    public Dialog getDialog() {
        String title = getArguments().getString(BundleValues.TITLE);
        String msg = getArguments().getString(BundleValues.MESSAGE);
        String positiveText = getArguments().getString(POSITIVE_TEXT);
        String negativeText = getArguments().getString(NEGATIVE_TEXT);

        Builder builder = new AlertDialog.Builder(getActivity(), R.style.Theme_Sherlock_Dialog);
        builder.setTitle(title)
                .setMessage(msg)
                .setPositiveButton(positiveText, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        mCallbacks.onDialogDismiss(true);
                    }
                })
                .setNegativeButton(negativeText, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        mCallbacks.onDialogDismiss(false);
                    }
                });

        return builder.create();
    }
}
